﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using MoiGioiHDBank.DTO;
using MoiGioiHDBank.DAO;
using System.Net;

namespace MoiGioiHDBank
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class MoiGioiHDBankWS : System.Web.Services.WebService
    {
        public static int phidichvucuamoigioi = 10;
        [WebMethod]
        public string Login(string userName, string password)
        {
            string kq = "Fail";
            Users us = UsersDAO.LayThongTinUser(userName, password);
            if (us != null && (us.ThongTinKhachHang.MaNhomKhachHang == 1 || us.ThongTinKhachHang.MaNhomKhachHang == 2))
            {
                SIDDTO sid = SIDDAO.LaySIDDTO(userName);
                if (sid != null && MineUtils.KiemTraTimeOut(sid.SID) == true) // co trong csdl va chua het timeout
                {
                    kq = sid.SID;
                }
                else
                {
                    kq = MineUtils.SIDGenerate();
                    SIDDAO.ThemSID(kq, userName, us.ThongTinKhachHang.MaNhomKhachHang, us.ThongTinKhachHang.SoThe);
                }
            }
            else
            {
                kq = "Not permission"; // sai username hoặc password
            }
            return kq;
        }


        [WebMethod]
        public int TranferMoneyForCustomer(string SID, string customerCreditCard, int amountMoney)
        {
            int kq = KiemTraTienToiThieuGui(amountMoney);
            if (kq > 0)
            {
                SIDDTO siduser = SIDDAO.LayThongTinSID(SID);
                if (siduser != null && MineUtils.KiemTraTimeOut(siduser.SID) == true)
                {
                    if (siduser.MaNhomKhachHang == 1)// kinh doanh
                    {
                        kq = ThucHienChuyenTien(SID, customerCreditCard, siduser.SoThe, amountMoney);
                    }
                    else
                    {
                        kq = -6;// nhom nxb ko duoc su dung
                    }                  
                }
                else
                {
                    kq = -1; //sid khong hop le
                }
            }
            return kq;
        }

        private int KiemTraTienToiThieuGui(int sotienthanhtoan)
        {
            if (sotienthanhtoan >= 10000 && sotienthanhtoan % 1000 == 0)
            {
                return 1; // thoa dieu kien ve tien chuyen
            }
            else if (sotienthanhtoan % 1000 == 0 && sotienthanhtoan < 10000)
            {
                return -12; // tien toi thieu chua dat
            }
            return -5; // khong phai la boi cua 1000
        }
        [WebMethod]
        public int TranferMoneyForProvider(string SID, string providerCreditCard, int amountMoney)
        {
            int kq = KiemTraTienToiThieuGui(amountMoney);
            if (kq > 0)
            {
                SIDDTO siduser = SIDDAO.LayThongTinSID(SID);
                if (siduser != null)
                {
                    if (MineUtils.KiemTraTimeOut(siduser.SID) == true && (siduser.MaNhomKhachHang == 1 || siduser.MaNhomKhachHang == 2))// kinh doanh hoac nha xuat ban
                    {
                        kq = ThucHienChuyenTien(SID, siduser.SoThe, providerCreditCard, amountMoney);
                    }
                }
                else
                {
                    kq = -1; //sid khong hop le
                }
            }
            return kq;
        }
        private int ThucHienChuyenTien(string sid, string theGui, string theNhan, int soTien)
        {
            int result = -1;

            CardSystem.EMVServices cs = new CardSystem.EMVServices();
            string tenNganHangGui = cs.GetBankCommercialNameOfCreditCard(theGui);

            LienKetNganHang moigioinganhanggui = LienKetNganHangDAO.LayThongTinNganHangLienKet(tenNganHangGui);
            if (moigioinganhanggui == null)
            {
                result = -10;
                // kq = " mooi goiwi hdb không hỗ trợ ngân hàng thẻ gửi";
            }
            else // có liên kết với ngân hàng thẻ gửi
            {
                result = GoiNganHangChuyenTien(theGui, theNhan, soTien, moigioinganhanggui);

                int sotienphicuamoigioi = (soTien * phidichvucuamoigioi) / 100;
                // tinh phi cua kinh doanh cho moi gioi
                result += GoiNganHangChuyenTien(theNhan, moigioinganhanggui.SoThe, sotienphicuamoigioi, moigioinganhanggui);
            }

            return result;
        }


        private int ThucHienChuyenPhiChoMoiGioi(string sothegui, string sothenhan, int sotien, LienKetNganHang moiGioiNganHangGui)
        {
            int kq = 0;
            kq = GoiNganHangChuyenTien(sothegui, sothenhan, sotien, moiGioiNganHangGui);
            return kq;
        }


        private int GoiNganHangChuyenTien(string soTheGui, string soTheNhan, int soTienGui, LienKetNganHang moiGioiNganHangGui)
        {
            int kq = -1;
            switch (moiGioiNganHangGui.TenNganHang)
            {
                case "MSB":
                    MaritimeBankWS.ServiceMaritimeBank wsbankMB = new MaritimeBankWS.ServiceMaritimeBank();
                    wsbankMB.CookieContainer = new CookieContainer();
                    string sidMB = wsbankMB.Login(moiGioiNganHangGui.UserName, moiGioiNganHangGui.Password);
                    kq = wsbankMB.Transfer(sidMB, soTheGui, soTheNhan, soTienGui);
                    return kq;
                case "NAB":
                    System.Net.ServicePointManager.Expect100Continue = false;
                    NamABankWS.Service1 wsbankNA = new NamABankWS.Service1();
                    wsbankNA.CookieContainer = new CookieContainer();
                    string sidNA = wsbankNA.Login(moiGioiNganHangGui.UserName, moiGioiNganHangGui.Password);
                    kq = wsbankNA.Tranfer(sidNA, soTheGui, soTheNhan, soTienGui);
                    return kq;
                default:
                    string sid = (string)WSProxy.CallWebService(moiGioiNganHangGui.URL, moiGioiNganHangGui.TenWebservice, "Login", new object[] { moiGioiNganHangGui.UserName, moiGioiNganHangGui.Password });
                    kq = (int)WSProxy.CallWebService(moiGioiNganHangGui.URL, moiGioiNganHangGui.TenWebservice, "Tranfer", new object[] { sid, soTheGui, soTheNhan, soTienGui });
                    return kq;
            }
        }
    }
}
